#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using NUnit.Framework;
using Utilities.Resources;
using System.Web;
using System.Drawing;
using Utilities.Testing;

namespace Utilities.Test.Resources
{
  public class ResourceLoaderFixture : TestFixtureBase
  {
    [Test]
    public void GetStringSimple()
    {
      string result;

      result = ResourceLoader<Mark>.GetString("ResourceLoaderTestSimple");
      Assert.AreEqual("TestValue", result);

      result = TestResourceLoader.GetString("ResourceLoaderTestSimple");
      Assert.AreEqual("TestValue", result);
    }

    [Test]
    public void GetStringFormat()
    {
      string result;

      result = ResourceLoader<Mark>.GetString("ResourceLoaderTestFormat", "Pie");
      Assert.AreEqual("Test format: 'Pie'", result);

      result = TestResourceLoader.GetString("ResourceLoaderTestFormat", "Pie");
      Assert.AreEqual("Test format: 'Pie'", result);
    }

    [Test]
    public void GetStringFromSystemWeb()
    {
      string result;

      result = ResourceLoader<HttpContext>.GetString("Adapter_GoLabel");
      Assert.AreEqual("Go", result);
    }

    [Test]
    public void GetStringEmpty()
    {
      string result;

      result = ResourceLoader<Mark>.GetString("ResourceLoaderTestEmpty");
      Assert.AreEqual(string.Empty, result);

      result = TestResourceLoader.GetString("ResourceLoaderTestEmpty");
      Assert.AreEqual(string.Empty, result);
    }

    [Test]
    [ExpectedException(typeof(Exception), ExpectedMessage = "Resource 'NotExistantResource' not found in Utilities.Test.Properties.Resources.")]
    public void GetStringNotExists()
    {
      ResourceLoader<Mark>.GetString("NotExistantResource");
    }

    [Test]
    [ExpectedException(typeof(Exception), ExpectedMessage = "Resource 'NotExistantResource' not found in Utilities.Test.Properties.Resources.")]
    public void GetStringNotExistsTestLoader()
    {
      TestResourceLoader.GetString("NotExistantResource");
    }

    [Test]
    public void GetObjectImage()
    {
      object result = TestResourceLoader.GetObject("IpodNanoBlue");
      Assert.IsNotNull(result);
      Assert.IsInstanceOfType(typeof(Bitmap), result);

      using (Bitmap bitmap = (Bitmap)result)
      {
        Size size = bitmap.Size;

        Assert.AreEqual(300, size.Width);
        Assert.AreEqual(300, size.Height);
      }
    }

    [Test]
    public void GetObjectIcon()
    {
      object result = TestResourceLoader.GetObject("NUnit");
      Assert.IsNotNull(result);
      Assert.IsInstanceOfType(typeof(Icon), result);

      using (Icon icon = (Icon)result)
      {
        Size size = icon.Size;

        Assert.AreEqual(32, size.Width);
        Assert.AreEqual(32, size.Height);
      }
    }

    [Test]
    public void GetObjectGeneric()
    {
      Icon icon = TestResourceLoader.GetObject<Icon>("NUnit");
      Assert.IsNotNull(icon);
    }

    [Test]
    [ExpectedException(typeof(Exception), ExpectedMessage = "Resource 'NUnit' is of type System.Drawing.Icon, not System.Drawing.Bitmap.")]
    public void GetObjectBadGeneric()
    {
      TestResourceLoader.GetObject<Bitmap>("NUnit");
    }
  }
}